////////////////////////////////
//funcdefine.lua
//This defines items that need functions to work, like commands or types
//WORKS
////////////////////////////////

//These functions actually utalize type checking, so the things above need to go first.
//Register commands:
/*
bbb.registercommand({
	name="", //Name of the command
	help="", //Help for the command
	!permissions=bbb.USER, //Permissions needed.
	func=function (flags) //Function for the command
		...
	end,
	!single="" //If no flags given, use this flag
	flags={
		flag1={
			type=bbb.FLAGTYPE, //Types it can be, add up the available types
			help="flag help" //The help text for that flag
			!optional=true/false //Whether it can be omitted
		}
		...
	}
})

An extra flag, 'as', is the user to run it as. This can be partial name or steamid or player object.
*/
bbb.registercommand({
	name="kick",
	help="Kicks a user.",
	permissions=bbb.MOD,
	func=function (flags)
		//With all users that are specified.
		for k,v in ipairs(bbb.fromstring(flags["u"],"user") or {}) do 
			game.ConsoleCommand("kickid "..v:SteamID().." \""..flags["m"].."\"\n")
			bbb.userdisconnect(v)
		end
	end,
	single="u",
	flags={
		u={
			type=bbb.t.userstring,
			help="User to kick.",
			optional=false
		},
		m={
			type=bbb.t.string,
			help="Message to tell the kicked user.",
			optional=true,
			default="Kicked by a Moderator."
		}
	}
})

bbb.registercommand({
	name="reload",
	help="Reload the bbb system.",
	permissions=bbb.ADMIN,
	func=function (flags)
		bbb.print(flags["m"],bbb.CONSOLE+bbb.USER,player.GetAll()) //Print the message.
		bbb.reload() //Reload the system.
	end,
	single="m",
	flags={
		m={
			type=bbb.t.string,
			help="Message to print when the system reloads.",
			optional=true,
			default="System restarting"
		}
	}
})

bbb.registercommand({
	name="unload",
	help="Unload the bbb system.",
	permissions=bbb.ADMIN,
	func=function (flags)
		bbb.print(flags["m"],bbb.CONSOLE+bbb.LOG+bbb.USER,{flags["as"]}) //Print the message.
		bbb.unload() //Unload the system.
	end,
	single="m",
	flags={
		m={
			type=bbb.t.string,
			help="Message to print when the system unloads.",
			optional=true,
			default="System unloading"
		}
	}
})

bbb.registercommand({
	name="set",
	help="Changes settings. Leave v blank to check the value.",
	permissions=bbb.ADMIN,
	func=function (flags)
		if(bbb.settings[flags["s"]]!=nil) then
			if(flags["v"]=="") then
				bbb.print(flags["s"]..": "..tostring(bbb.settings[flags["s"]]),bbb.USER,{flags["as"]})
			else
			local stype=type(bbb.settings[flags["s"]]) //Get the type of the setting.
				local converted=bbb.fromstring(flags["v"],stype) //Convert it to that type.
				if(bbb.is(converted,stype)==true) then //Check if they are the same type
						bbb.set(flags["s"],converted)
				bbb.print("Set '"..flags["s"].."' to: '"..tostring(converted).."'",bbb.USER,{flags["as"]})
				else
					bbb.print("The setting value types must match: "..stype,bbb.USER,{flags["as"]})
				end
			end
		else
			//It should never reach this point, due to previous type checking.
			bbb.print("That setting does not exist.",bbb.USER,{flags["as"]})
		end
	end,
	single="s",
	flags={
		s={
			type=bbb.t.setting,
			help="Setting to change.",
			optional=false,
		},
		v={
			type=bbb.t.string,
			help="Value to set to.",
			optional=true,
			default="",
		}
	}
})

bbb.registercommand({
	name="help",
	help="Prints help information.",
	func=function (flags)
		if(flags["i"]=="help") then //No help, just print all.
			for k,v in pairs(bbb.commands) do
				bbb.print(k.." - "..v.help,bbb.USER,{flags["as"]}) //Print command
				for k2,v2 in pairs(v.flags) do
					bbb.print("	"..k2.." - "..v2.help,bbb.USER,{flags["as"]}) //Print flag and flag help
				end
			end
		else
			if(bbb.commands[flags["i"]]==nil) then
				bbb.print("'"..flags["i"].."' is a non-existant command.",bbb.USER,{flags["as"]})
			else
				local k,v
				k=flags["i"] //Nab the command
				v=bbb.commands[k] //Nab the table
				bbb.print(k.." - "..v.help,bbb.USER,{flags["as"]}) //Print command
				for k2,v2 in pairs(v.flags) do
					bbb.print("	"..k2.." - "..v2.help,bbb.USER,{flags["as"]}) //Print flag and flag help
				end
			end
		end
	end,
	single="i",
	flags={
		i={
			type=bbb.t.command,
			help="Help for item.",
			optional=true,
			default="help"
		}
	}
})

bbb.registercommand({
	name="mysql",
	help="Runs a MySQL command on the server.",
	permissions=bbb.OWN,
	func=function (flags)
		bbb.tprint(bbb.query(flags["c"]),bbb.USER,{flags["as"]})
	end,
	single="c",
	flags={
		c={
			type=bbb.t.string,
			help="Command to run.",
			optional=false,
		}
	}
})